package com.zhengdongzulin.zuke.app.main.apartment.sign

import android.content.Intent
import android.view.View
import android.widget.TextView
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.picker.widget.TimePickerView
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder
import com.xuexiang.xui.widget.picker.widget.configure.TimePickerType
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.ContractListData
import com.zhengdongzulin.zuke.app.data.PayMethod
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivitySign1Binding
import com.zhengdongzulin.zuke.ext.getTextZ
import com.zhengdongzulin.zuke.ext.put
import com.zhengdongzulin.zuke.ext.setDrawRight
import com.zhengdongzulin.zuke.ext.start
import com.zhengdongzulin.zuke.utils.DateFormatConstants
import com.zhengdongzulin.zuke.utils.DateUtils
import java.text.SimpleDateFormat
import java.util.*


class ZKSign1Activity : BaseActivity<SignVm, ZkActivitySign1Binding>() {

    private var channelListTemp = mutableListOf<PayMethod>()
    var mData = mutableListOf<ContractListData>()
    lateinit var mHouseId: String

    var mTermList = mutableListOf<PayMethod>()
    var mTermPosition = 0
    var mChannelSelectPosition = 0
    var mTermIsOpen = false
    var mPayMethodList = mutableListOf<PayMethod>()
    var mPayMethodListAll = mutableListOf<PayMethod>()
    var mPayMethodPosition = 0
    var mPayMethodIsOpen = false
    var mChannelIsOpen = false
    var mTimePicker: TimePickerView? = null
    var startDate = ""
    var endDate = ""
    var name = ""
    var idCardNo = ""

    override fun getLayoutResId(): Int = R.layout.zk_activity_sign1

    override fun initVM(): SignVm = getVM(SignVm::class.java)

    override fun initView() {
        setTitleContent("在线签约")
        mView.statusView.showLoading()
        mHouseId = intent.getStringExtra("houseId")!!
        mViewModel.get_sign_house_info(mHouseId)
        mViewModel.get_sign_channel_source_dic_list()
    }

    override fun initData() {
    }

    override fun initListener() {
        mView.clTime.setOnClickListener {
            mTimePicker?.show()
        }
        mView.clSignTerm.setOnClickListener {
            mTermIsOpen = !mTermIsOpen
            if (mTermIsOpen) {
                mView.recyclerView1.visibility = View.VISIBLE
                mView.clSignTerm.setBackgroundColor(getColor(R.color.ffeccc))
                mView.tvSignTerm.setTextColor(getColor(R.color.color_1273f7))
                mView.tvSignTermTitle.setTextColor(getColor(R.color.color_1273f7))
                mView.tvSignTermTitle.setDrawRight(R.mipmap.up_triangle_sel)
            } else {
                mView.recyclerView1.visibility = View.GONE
                mView.clSignTerm.setBackgroundColor(getColor(R.color.white))
                mView.tvSignTerm.setTextColor(getColor(R.color.gray_99))
                mView.tvSignTermTitle.setTextColor(getColor(R.color.black_33))
                mView.tvSignTermTitle.setDrawRight(R.mipmap.down_triangle)
            }
        }
        mView.clSignPayMethod.setOnClickListener {
            mPayMethodIsOpen = !mPayMethodIsOpen
            if (mPayMethodIsOpen) {
                mView.recyclerView2.visibility = View.VISIBLE
                mView.clSignPayMethod.setBackgroundColor(resources.getColor(R.color.ffeccc))
                mView.tvSignPayMethod.setTextColor(getColor(R.color.color_1273f7))
                mView.tvSignPayMethodTitle.setTextColor(getColor(R.color.color_1273f7))
                mView.tvSignPayMethodTitle.setDrawRight(R.mipmap.up_triangle_sel)
            } else {
                mView.recyclerView2.visibility = View.GONE
                mView.clSignPayMethod.setBackgroundColor(resources.getColor(R.color.white))
                mView.tvSignPayMethod.setTextColor(resources.getColor(R.color.gray_99))
                mView.tvSignPayMethodTitle.setTextColor(resources.getColor(R.color.black_33))
                mView.tvSignPayMethodTitle.setDrawRight(R.mipmap.down_triangle)
            }
        }
        mView.clSignChannel.setOnClickListener {
            mChannelIsOpen = !mChannelIsOpen
            if (mChannelIsOpen) {
                mView.recyclerView3.visibility = View.VISIBLE
                mView.clSignChannel.setBackgroundColor(resources.getColor(R.color.ffeccc))
                mView.tvSignChannel.setTextColor(getColor(R.color.color_1273f7))
                mView.tvSignChannelTitle.setTextColor(getColor(R.color.color_1273f7))
                mView.tvSignChannelTitle.setDrawRight(R.mipmap.up_triangle_sel)
            } else {
                mView.recyclerView3.visibility = View.GONE
                mView.clSignChannel.setBackgroundColor(resources.getColor(R.color.white))
                mView.tvSignChannel.setTextColor(resources.getColor(R.color.gray_99))
                mView.tvSignChannelTitle.setTextColor(resources.getColor(R.color.black_33))
                mView.tvSignChannelTitle.setDrawRight(R.mipmap.down_triangle)
            }
        }
        mView.tvSign1ShowDetail.setOnClickListener {
            if (Next()) return@setOnClickListener
            Intent().put("houseId", mHouseId)
                .put("rentTypeId", mTermList[mTermPosition].id)
                .put("payTypeId", mPayMethodList[mPayMethodPosition].id)
                .put("startDate", startDate)
                .put("endDate", endDate)
                .put("name", name)
                .put("idCardNo", idCardNo)
                .put("startDate2", mView.time.getTextZ())
                .start(this@ZKSign1Activity, ZKSign2Activity::class.java)
        }
        mView.next.setOnClickListener {
            if (Next()) return@setOnClickListener
            Intent().put("houseId", mHouseId)
                .put("payTypeId", mPayMethodList[mPayMethodPosition].id)
                .put("ChannelSourceId", channelListTemp[mChannelSelectPosition].id.toString())
                .put("startDate", startDate)
                .put("endDate", endDate)
                .put("name", name)
                .put("idCardNo", idCardNo)
                .start(this@ZKSign1Activity, ZKSign3Activity::class.java)
        }
    }

    private fun Next(): Boolean {
        if (mView.time.getTextZ() == "请选择日期") {
            toast("请选择日期")
            return true
        }
        if (mView.tvSignTerm.getTextZ() == "请选择租房期限") {
            toast("请选择租房期限")
            return true
        }
        if (mView.tvSignPayMethod.getTextZ() == "请选择付款方式") {
            toast("请选择付款方式")
            return true
        }
        if (mView.tvSignChannel.getTextZ() == "请选择渠道来源") {
            toast("请选择渠道来源")
            return true
        }
        return false
    }

    override fun startObserve() {
        mViewModel.get_sign_house_info.observe(this, androidx.lifecycle.Observer {
            name = it.name
            idCardNo = it.idCardNo
            mTermList.addAll(it.checkInTimeList)
            mPayMethodListAll.addAll(it.payMethodList)
            mView.tvSignTerm.text = mTermList[mTermPosition].name
            for (payMethod in mPayMethodListAll) {
                if (payMethod.value <= mTermList[mTermPosition].value) {
                    mPayMethodList.add(payMethod)
                }
            }
            mView.tvSignPayMethod.text = mPayMethodList[mPayMethodPosition].name
            mView.recyclerView1.visibility = View.GONE
            mView.recyclerView2.visibility = View.GONE
            mView.tvHouseName.text = "${it.houseName}"
            mView.tvAddress.text = "${it.houseAddress}"
            mView.tvDoorName.text = "${it.doorName}"
            mView.tvArea.text = "${it.area}㎡"
            mView.tvRentPrice.text = "${it.rentPrice}元"
            mView.tvPropertyPrice.text = "${it.propertyPrice}元"
            mView.time.text = "${it.startDate}"
            mView.tvDate.text = "${it.startDate}至${it.endDate}"
            mView.recyclerView1.vertical().bindData(
                mTermList,
                R.layout.item_sign1_text
            ) { holder, item, position ->
                holder.setText(R.id.name, item.name)
                holder.getView<TextView>(R.id.name)
                    .setTextColor(
                        getColor(
                            if (mTermPosition == position) R.color.color_1273f7 else R.color.color_7a7974
                        )
                    )
                holder.itemView.setOnClickListener {
                    mTermIsOpen = !mTermIsOpen
                    mTermPosition = position
                    mView.recyclerView1.adapter!!.notifyDataSetChanged()
                    mView.recyclerView1.gone()
                    mView.tvSignTerm.text = mTermList[mTermPosition].name
                    mPayMethodList.clear()
                    for (payMethod in mPayMethodListAll) {
                        if (payMethod.value <= mTermList[mTermPosition].value) {
                            mPayMethodList.add(payMethod)
                        }
                    }
                    mView.recyclerView2.adapter!!.notifyDataSetChanged()
                    mView.tvSignPayMethod.text = "请选择付款方式"
                    mView.price.text = "0.00元"
                }
            }

            mView.recyclerView2.vertical().bindData(
                mPayMethodList,
                R.layout.item_sign1_text
            ) { holder, item, position ->
                holder.setText(R.id.name, item.name)
                holder.getView<TextView>(R.id.name)
                    .setTextColor(
                        resources.getColor(
                            if (mPayMethodPosition == position) R.color.color_1273f7 else R.color.color_7a7974
                        )
                    )
                holder.itemView.setOnClickListener {
                    mPayMethodIsOpen = !mPayMethodIsOpen
                    mPayMethodPosition = position
                    mView.recyclerView2.adapter!!.notifyDataSetChanged()
                    mView.recyclerView2.gone()
                    mView.tvSignPayMethod.text = mPayMethodList[mPayMethodPosition].name
                    getFirstMoney()
                }
            }
            val calendar: Calendar = Calendar.getInstance()
            calendar.setTime(
                DateUtils.millis2Date(
                    DateUtils.string2Millis(
                        "2000-01-01",
                        SimpleDateFormat(DateFormatConstants.yyyyMMdd)
                    )
                )
            )
            var calendar2 = Calendar.getInstance()
            calendar2.setTime(
                DateUtils.millis2Date(
                    DateUtils.string2Millis(
                        "2122-01-01",
                        SimpleDateFormat(DateFormatConstants.yyyyMMdd)
                    )
                )
            )

            mTimePicker = TimePickerBuilder(
                this
            ) { date, v ->
                mView.time.text =
                    DateUtils.date2String(date, SimpleDateFormat(DateFormatConstants.yyyyMMdd))
                getFirstMoney()
            }
                .setTimeSelectChangeListener { }
                .setType(TimePickerType.DEFAULT)
                .setCancelColor(color(R.color.color_1273f7))
                .setSubmitColor(color(R.color.color_1273f7))
                .setTitleColor(color(R.color.color_1273f7))
                .setDividerColor(color(R.color.color_1273f7))
                .setTextColorCenter(color(R.color.color_1273f7))
                .setTitleText("选择入住时间")
                .setOutSideCancelable(false)
                .setDate(Calendar.getInstance().apply {
                    time = DateUtils.millis2Date(System.currentTimeMillis())
                })
                .setRangDate(calendar, calendar2)
                .build()
            mView.statusView.showContent()
        })
        mViewModel.get_sign_house_info.state.observe(
            this,
            getObserver(mViewModel.get_sign_house_info)
        )
        mViewModel.get_sign_channel_source_dic_list.observe(this) {
            channelListTemp = it
            mView.recyclerView3.vertical().bindData(
                channelListTemp,
                R.layout.item_sign1_text
            ) { holder, item, position ->
                holder.setText(R.id.name, item.name)
                holder.getView<TextView>(R.id.name)
                    .setTextColor(
                        getColor(
                            if (mChannelSelectPosition == position) R.color.color_1273f7 else R.color.color_7a7974
                        )
                    )
                holder.itemView.setOnClickListener {
                    mChannelIsOpen = !mChannelIsOpen
                    mChannelSelectPosition = position
                    mView.recyclerView3.adapter!!.notifyDataSetChanged()
                    mView.recyclerView3.gone()
                    mView.tvSignChannel.text = channelListTemp[mChannelSelectPosition].name
                }
            }
        }
        mViewModel.get_sign_channel_source_dic_list.state.observe(
            this,
            getObserver(mViewModel.get_sign_channel_source_dic_list)
        )
        mViewModel.get_first_money.observe(this, androidx.lifecycle.Observer {
            mView.price.text = "${it.firstMoney}元"
            startDate = it.startDate
            endDate = it.endDate
            mView.tvDate.text = "${it.startDate}至${it.endDate}"
        })
        mViewModel.get_first_money.state.observe(
            this,
            getObserver(mViewModel.get_first_money)
        )
    }

    fun getFirstMoney() {
        if (mView.tvSignTerm.getTextZ() == "请选择租房期限" || mView.tvSignPayMethod.getTextZ() == "请选择付款方式" || mView.time.getTextZ() == "请选择日期") {
            return
        }
        mViewModel.get_first_money(
            mHouseId,
            rentTypeId = mTermList[mTermPosition].id,
            payTypeId = mPayMethodList[mPayMethodPosition].id,
            startDate = mView.time.getTextZ()
        )
    }

}